<?php

require_once 'base/base_categories.class.php';
require_once 'UserPermissionCategory.php';

/**
 *  zend db table
 */
class Application_Model_DbTable_UserPermissionCategories extends Zend_Db_Table_Abstract
{
    protected $_name = 'ap_user_permission_category';
}


/**
 *  
 */
class UserPermissionCategories extends BaseCategories
{
    protected $_modelName             = 'UserPermissionCategory';
    protected $_dbTableName           = 'Application_Model_DbTable_UserPermissionCategories';

    /* ================================================================================
        access database
    ================================================================================ */

    /**
     *  add 
     */
    public function addUserPermissionCategory( $model )
    {
        return $this->addCategory( $model );
    }

    /**
     *  update 
     */
    public function updateUserPermissionCategory( $model )
    {
        return $this->updateCategory( $model );
    }

    /**
     *  get 
     */
    public function getUserPermissionCategory( $id, $userId=null, $status=UserPermissionCategory::STATUS_ALL )
    {
        return $this->getCategory( $id, $userId, $status );
    }

    /**
     *  get many 
     */
    public function getUserPermissionCategories( $searchKey=null, $status=BaseCategory::STATUS_OPEN, $page=1, $rowCount=BaseMapper::DEFAULT_ROW_COUNT ) 
    {
        return $this->getCategories( $searchKey, $status, $page, $rowCount );
    }

    /**
     *  get count by "getUserPermissionCategories" method
     */
    public function getNumUserPermissionCategories( $searchKey=null ) 
    {
        return $this->getNumCategories( $searchKey, $status );
    }

    /**
     *  get many UserPermissionCategories by link table
     */
    public function getUserPermissionCategoriesByUserId( $userId )
    {
        $userPermissionCategoryLinks = new UserPermissionCategoryLinks();
        $myPermissionLinks = $userPermissionCategoryLinks->findUserPermissionCategoryLinks( null, $userId );
        if( !$myPermissionLinks ) {
            return Array();
        }

        $myPermissions = Array();
        foreach( $myPermissionLinks as $myPermissionLink ) {
            $categoryId = $myPermissionLink->getCategoryId();
            $myPermissions[] = $this->getUserPermissionCategory( $categoryId );
        }
        return $myPermissions;
    }


    /* ================================================================================
        about model object
    ================================================================================ */

    /**
     *  set mapRow by pure database record
     */
    public function mapRow($row)
    {
        return parent::mapRow($row);
    }

}

